"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var UIComponent_1 = require("../basic/models/UIComponent");
var ElementHelper = require("../utils/ElementHelper");
var CastHelper = require("../utils/CastHelper");
var TypeHelper = require("../utils/TypeHelper");
var ExceptionHelper = require("../utils/ExceptionHelper");
var UITabContent = (function (_super) {
    __extends(UITabContent, _super);
    function UITabContent(app, id, url) {
        var _this = _super.call(this, app) || this;
        _this.id = id;
        _this.url = url;
        _this._element = null;
        _this._contentWindow = null;
        if (!TypeHelper.isStringWithNotEmpty(_this.id))
            ExceptionHelper.assertId();
        _this.url = CastHelper.stringCast(_this.url);
        _this.elementId = _this.app.prefix + "tab-content" + "-" + _this.id;
        return _this;
    }
    Object.defineProperty(UITabContent.prototype, "element", {
        get: function () {
            return document.getElementById(this.elementId);
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(UITabContent.prototype, "contentWindow", {
        get: function () {
            var frameElement = document.querySelector("#" + this.elementId + " ." + (this.app.prefix + "tab-content-frame"));
            if (!frameElement)
                return null;
            return frameElement.contentWindow;
        },
        enumerable: true,
        configurable: true
    });
    UITabContent.prototype.present = function () {
        var fragment = ElementHelper.createFragment();
        var tabContentElement = ElementHelper.createElement("div");
        tabContentElement.id = this.elementId;
        ElementHelper.addClasses(tabContentElement, this.app.prefix, "tab-content");
        var frameContentElement = ElementHelper.createElement("iframe");
        frameContentElement.setAttribute("data-tab-id", this.id);
        frameContentElement.setAttribute("frameborder", "0");
        frameContentElement.setAttribute("scrolling", "no");
        frameContentElement.setAttribute("allowtransparency", "true");
        frameContentElement.src = this.url;
        ElementHelper.addClasses(frameContentElement, this.app.prefix, "tab-content-frame");
        this.bindEvent(frameContentElement);
        tabContentElement.appendChild(frameContentElement);
        fragment.appendChild(tabContentElement);
        return fragment;
    };
    UITabContent.prototype.bindEvent = function (frameContentElement) {
        var _this = this;
        frameContentElement.addEventListener("load", function (ev) {
            var contentWindow = frameContentElement.contentWindow;
            if (!contentWindow)
                return;
            var item = _this.app.item;
            if (item && item.useSubTitle) {
                var subTitleElement = contentWindow.document.querySelector("title");
                if (subTitleElement) {
                    item.updateTitle(subTitleElement.innerText || "未命名标题");
                }
            }
        });
    };
    return UITabContent;
}(UIComponent_1.default));
exports.default = UITabContent;
